<template>
	<view>
		<tabs :list="list" :active="active" @tabBtn="tabBtn" class="hd-height"></tabs>
		<view class="p-b-120">
			<list :orderList="orderList" :height="scrollviewHigh" @scrolltolower="scrolltolower" @delorder="delorder"
				@okOrder="okOrder" @uncheckorder="uncheckorder" @sureOrder="sureOrder" @eva="eva"
				@seeOrderDetail="seeOrderDetail"></list>
		</view>
		<carBottoms :type2="false" class="bottom-height"></carBottoms>
	</view>
</template>

<script>
	import tabs from './components/tabs.vue'
	import list from './components/list.vue'
	export default {
		components: {
			tabs,
			list
		},
		data() {
			return {
				scrollviewHigh: "",
				active: 0,
				list: ['等待接单', '已接单', '已完成', '已取消'],
				orderList: [],
				page: 1,
				Interval: ','
			}
		},
		onReady() {
			this.getHeight()
		},
		onShow() {
			this.getList(this.page, this.active)
			var that = this
			this.Interval = setInterval(function() {
				that.getList(that.page, that.active)
			}, 1000)
		},
		destroyed() {
			clearInterval(this.Interval)
		},
		methods: {
			getHeight() {
				let _this = this
				uni.getSystemInfo({
					success(res) {
						let view = uni.createSelectorQuery().select(".hd-height")
						view.boundingClientRect(data => {
							_this.scrollviewHigh = res.windowHeight - data.height - 58
							console.log(_this.scrollviewHigh)
						}).exec()
					}
				})
			},
			async getList(page, key) {
				let data = await this.$request.post('/api/product/list', {
					page: page || 1,
					status: key
				})
				this.orderList = []
				console.log(data)
				let list = data.data.data.data
				for (let i in list) {
					this.orderList.push(list[i])
				}
			},
			async delorder(id) {
				console.log(id)
				let data = await this.$request.post('/api/product/delorder', {
					oid: id
				})
				console.log(data)
				if (data.data.code === 1) {
					uni.showToast({
						title: "订单已取消",
						icon: "none"
					})
					this.orderList = []
					this.getList(1, this.active)
				}
			},
			async okOrder(id) {
				let _this = this
				let result = await this.$request.post('/api/product/price_changeprice_change', {
					oid: id
				})
				console.log(result)
				if (result.data.code === 2) {
					uni.showModal({
						title: '重要提示',
						content: result.data.msg,
						confirmText: "完成订单",
						cancelText: "取消订单",
						success: function(res) {
							if (res.confirm) {
								_this.sureOrderStatic(id)
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				} else {
					_this.sureOrderStatic(id)
				}
			},
			async sureOrderStatic(id) {
				let data = await this.$request.post('/api/product/doneOrder', {
					oid: id
				})
				console.log(data)
				if (data.data.code === 1) {
					uni.showToast({
						title: "订单已完成",
						icon: "none"
					})
					this.orderList = []
					this.getList(1, this.active)
				} else if (data.data.code === 2) {
					uni.showToast({
						title: '请先对司机进行评价，而后完成订单操作',
						icon: "none"
					})
					setTimeout(() => {
						uni.navigateTo({
							url: "/pages/carOwner/evaDrive/index?id=" + id + "&type=" + 0
						})
					}, 1500)
				} else {
					uni.showToast({
						title: data.data.msg,
						icon: "none"
					})
				}
			},
			async uncheckorder(id) {
				let data = await this.$request.post('/api/product/uncheckorder', {
					oid: id
				})
				console.log(data)
				if (data.data.code === 1) {
					uni.showToast({
						title: "操作成功，请重新等待接单司机",
						icon: "none"
					})
					this.orderList = []
					this.getList(1, this.active)
				}
			},
			async sureOrder(id) {
				let data = await this.$request.post('/api/product/checkorder', {
					order_id: id
				})
				console.log(data)
				uni.showToast({
					title: data.data.msg,
					icon: "none"
				})
				if (data.data.code === 1) {

					this.orderList = []
					this.getList(1, this.active)
				}
			},
			seeOrderDetail(id) {
				uni.navigateTo({
					url: "/pages/orderDetail/index?id=" + id + "&type=" + 0
				})
			},
			eva(id) {
				uni.navigateTo({
					url: "/pages/carOwner/evaDrive/index?id=" + id
				})
			},
			tabBtn(key) {
				this.active = key
				this.orderList = []
				this.getList(1, key)
			},
			scrolltolower() {
				setTimeout(() => {
					this.getList(++this.page, this.active)
				}, 1000)
			}
		}
	}
</script>

<style lang="scss">
	.driver-info {
		position: absolute;
		top: 0;
		left: 0;
	}
</style>
